#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName  :huaban_boards.py
# @Time      :2023/7/27 
# @Author    :CL
# @email     :1037654919@qq.com

import os
import time
import random
from tqdm import tqdm
from utils import MongoDBUtil, get_html, mongo_manager
from multiprocessing import Pool
from huaban_util import Huaban

MongoDBUtil = MongoDBUtil()


def get_proxy():
    port = random.randint(24000, 24400)
    proxies = {'http': f'http://zheng123:zheng123@haproxy.iinti.cn:{port}',
               'https': f'http://zheng123:zheng123@haproxy.iinti.cn:{port}'}
    return proxies


def insert_data(tablename, datas):
    if len(datas) > 0 and datas is not None:
        for data in datas:
            # data['_id'] = data['id']
            try:
                # print(data)
                MongoDBUtil.insert_one(tablename, data)
            except Exception as e:
                print(e)
    else:
        pass


def get_boards(tuple):
    # print(tuple[0],tuple[1])
    keyword = tuple[0]
    # add
    keyword = tuple[1]
    value = tuple[1]
    response = huaban.get_board_id(keyword, 1)
    board_count = response['board_count']
    pages = int(board_count) / 20
    print(keyword, ':', pages, '页')
    for page in range(1, int(pages) + 1):
        response = huaban.get_board_id(keyword, page)
        datas = response['boards']
        print('len(datas):', len(datas))
        for data in datas:
            result = {}
            result['_id'] = data['board_id']
            result['board_id'] = data['board_id']
            result['keyword'] = keyword
            result['keyword_e'] = value
            result['page'] = page
            result['user_id'] = data['user_id']
            result['title'] = data['title']
            result['pin_count'] = data['pin_count']
            result['seq'] = data['seq']
            try:
                # print(data)
                MongoDBUtil.insert_one('huaban_boards', result)
            except Exception as e:
                print(e)
            # break
        if len(datas) < 20:
            break

    return


if __name__ == "__main__":
    huaban = Huaban()
    # play_list = kuwo.get_play_list(1, 20)
    # keyword = 'car'
    # page = 1
    # res = huaban.get_board_id(keyword,page)
    # print(res)
    keywords = {"概念车": "concept car", "未来汽车": "future car", "未来派汽车": "futuristic car",
                "汽车": "car", "漂亮汽车": "beautiful car", "汽车造型": "automotive styling",
                "创意汽车": "creative car",
                "工业汽车": "industrial car", "汽车外观": "automotive exterior",
                "汽车概念": "automotive concept",
                "科技感汽车": "tech-inspired car", "现代汽车": "modern car",
                "未来主义汽车": "futurism car", "实验性汽车": "experimental car",
                "先进汽车": "advanced car", "独特汽车": "unique car",
                "未来导向汽车": "future-oriented car", "高科技汽车": "high-tech car",
                "创新汽车": "innovative car", "前瞻性汽车": "forward-looking car",
                "前沿汽车": "cutting-edge car", "概念性车辆": "conceptual vehicle",
                "未来出行工具": "future mobility tool",
                "汽车美学": "automotive aesthetics",
                "概念车模型": "concept car model",
                "汽车概念模型": "car conceptual model", "未来出行交通工具": "future mobility vehicle",
                "汽车造型艺术": "automotive styling art",
                "车身": "vehicle body", "汽车构造": "automotive structure",
                "车辆外观造型": "vehicle exterior styling", "车辆内饰": "vehicle interior"}

    pool = Pool(processes=30)
    pool.map(get_boards, keywords.items())
    pool.close()  # 关闭进程池，不再接受新的进程
    pool.join()

    # # {"未来汽车": "future car"}
    # get_boards("未来汽车", "future car")
